/*
 * LedAnimator.c
 * @brief	
 * 
 * @deatils
 *
 * @Module_Owner Irbor	
 *
 * Copyright 2014 Bober Company
 *  
 */

//----------------------------------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------------------------------
#include "LedAnimator.h"
#include "LedAnimator_prv.h"

//----------------------------------------------------------------------------------------------------
// Static variables
//----------------------------------------------------------------------------------------------------
static ANIMATION_STATE_TYPE Animation_State;
static unsigned char Step;
static unsigned int  Time;
static unsigned char Anim_Index;
static unsigned char Repeats;
static unsigned char Repeating;

static LED_GROUP_TYPE* Led_Groups[NUM_OF_LEDS_GROUPS] = {Led_Groups};

static Running_Animations

//----------------------------------------------------------------------------------------------------
// Private Functions Declarations
//----------------------------------------------------------------------------------------------------
void Set_Led_Pattern(LED_GROUP_TYPE led_group, unsigned char mask);
void Turn_All_Leds_Off(LED_GROUP_TYPE led_group);
void Animate(ANIMATION_STEP_TYPE* Animation);
//----------------------------------------------------------------------------------------------------
// Public Functions
//----------------------------------------------------------------------------------------------------

void LedAnimator_Background(void)
{
	unsigned int index;
	
	for(index = 0; index < ((Running_Animations < MAX_RUNNING_ANIMATIONS) ? Running_Animations : MAX_RUNNING_ANIMATIONS); index ++)
	{
		Animate(
	}
}

void LedAnimator_Animate(void)
{
	switch(Animation_State)
	{
	case IDLE:
		Step = 0;
		Time = ((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->time;
		Set_Led_Pattern(((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->mask);
		Animation_State = RUNNING;
		break;

	case RUNNING:
		Time--;
		if(Time == 0)
		{
			Step++;
			if(((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->event == STOP)
			{
				Animation_State = LAST_STEP;
			}
			else if((((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->event == REPEAT))
			{
				if(Repeating == 0)
				{
					Repeats = ((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->param;
					Repeating = 1;
					Step = 0;
				}
				else
				{
					Repeats--;
					if(Repeats == 0)
					{
						Animation_State = LAST_STEP;
					}
					else
					{
						Step = 0;
					}
				}
			}
			Time = ((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->time;
			Set_Led_Pattern(((ANIMATION_STEP_TYPE*)(Animations[Anim_Index] + Step))->mask);
		}

		break;
	case LAST_STEP:
		Time--;
		if(Time == 0)
		{
			Repeating = 0;
			Animation_State = FINISHED;
		}
		break;
	case FINISHED:
		Turn_All_Leds_Off();
		if(STM_EVAL_PBGetState(BUTTON_USER) == 1)
		{
			Anim_Index++;
			if(Anim_Index >= NUM_OF_ANIMATIONS)
			{
				Anim_Index = 0;
			}
			Animation_State = IDLE;
		}
		break;

	}
}
//---------------------------------------------------------------------------------------------------
// Private functions
//---------------------------------------------------------------------------------------------------


void Set_Led_Pattern(LED_GROUP_TYPE led_group, unsigned char mask)
{
	unsigned int index;

	for(index = 0;index < NUM_OF_CIRCLE_LEDS; index++)
	{
		if(mask & (1 << index))
		{
			STM_EVAL_LEDOn(LedsTab[index]);
		}
		else
		{
			STM_EVAL_LEDOff(LedsTab[index]);
		}
	}
}

/**
 * @brief Turns all leds in circle
 *
 */
void Turn_All_Leds_Off(LED_GROUP_TYPE led_group)
{
	unsigned int index;
	for(index = 0; index < NUM_OF_CIRCLE_LEDS; index++)
	{
		STM_EVAL_LEDOff(LedsTab[index]);
	}
}
